package net

import (
	"fmt"
	"golang.org/x/net/html"
	"log"
	"net/http"
)

// 解析html
func ParseDemo() {
	resp, err := http.Get("https://yz.chsi.com.cn/sch/")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	doc, err := html.Parse(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	// 定义一个存储院校的切片
	schoolSlice := make([]string, 20, 200)

	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "a" {
			for _, attr := range n.Attr {
				if attr.Key == "class" && attr.Val == "name js-yxk-yxmc text-decoration-none" {
					schoolSlice = append(schoolSlice, n.FirstChild.Data)
				}
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}

	// 调用匿名函数
	f(doc)

	fmt.Println(schoolSlice)
}
